#!/usr/bin/python
# coding=utf-8

"""批量打包

"""
__author__ = 'x'
__email__ = 'x@gmail.com'
VERSION = "v1.0"

import os, sys, re, time, getopt, string, datetime
import shutil
import glob
import Image
import ConfigParser
import zipfile
import md5
import json

tihuan = []
info = []

opts, args = getopt.getopt(sys.argv[1:], "hs:t:", ["help", "source", "target"])


is_has_source_path = False
source_path = ""

is_has_target_path = False
target_path = ""


def usage():
    print ("Usage:xxxx [ops]")

if not opts:
    usage()
    sys.exit()
else:
    for op, value in opts:
        print "op:value="+op+":"+value
        if op == "-s" or op == "--source":
            is_has_source_path = True
            source_path = value
        elif op == "-t" or op == "--target":
            is_has_target_path = True
            target_path = value
        elif op == "-h" or op == "--help":
            usage()
            sys.exit()

text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b"))
_null_trans = string.maketrans("", "")

def istextfile(filename, blocksize = 512):
    fileread = open(filename,'r');
    filer_tmp_str = fileread.read(blocksize);
    fileread.close();
    return istext(filer_tmp_str)

def istext(s):
    if "\0" in s:
        return 0

    if not s:  # Empty files are considered text
        return 1

    # Get the non-text characters (maps a character to itself then
    # use the ‘remove’ option to get rid of the text characters.)
    t = s.translate(_null_trans, text_characters)

    # If more than 30% non-text characters, then
    # this is considered a binary file
    if len(t)/len(s) > 0.30:
        return 0
    return 1

#解压zip文件
def unzip():
    source_zip="c:\\update\\SW_Servers_20120815.zip"
    target_dir="c:\\update\\"
    myzip=ZipFile(source_zip)
    myfilelist=myzip.namelist()
    for name in myfilelist:
        f_handle=open(target_dir+name,"wb")
        f_handle.write(myzip.read(name))
        f_handle.close()
    myzip.close()

#添加文件到已有的zip包中
def addzip():
    f = zipfile.ZipFile('archive.zip','a',zipfile.ZIP_DEFLATED)
    f.write('file_to_add.py')
    f.close()

#把整个文件夹内的文件打包
def adddirfile(the_zipfile, the_dir, the_append_bale_root_path):
    f = zipfile.ZipFile(the_zipfile,'a',zipfile.ZIP_DEFLATED)
    startdir = the_dir
    for dirpath, dirnames, filenames in os.walk(startdir):
        b = dirpath.replace(the_append_bale_root_path,'')
#        strinfo = re.compile('\\bale_need_data')
#        b = strinfo.sub('',dirpath)
#        print "dirpath==="+dirpath
        for filename in filenames:
            b_file = os.path.join(b,filename)
            f.write(os.path.join(dirpath,filename), b_file)
    f.close()

def main():
    append_bale_root_path = source_path;
    append_bale_dirs = [append_bale_root_path+"\\extra"]

    for i_i in append_bale_dirs:
        adddirfile(target_path, i_i, append_bale_root_path)
    print "Good job!"

if __name__ == "__main__":
  sys.exit(main())
